<template>
  <div class="api-debug">
    <h1>API连接调试页面</h1>
    <p>用于诊断API连接和配置问题</p>
    
    <div class="debug-sections">
      <!-- API配置信息 -->
      <div class="debug-section">
        <h3>API配置信息</h3>
        <div class="info-grid">
          <div v-for="(value, key) in apiStatus" :key="key" class="info-item">
            <strong>{{ key }}:</strong> {{ typeof value === 'object' ? JSON.stringify(value) : value }}
          </div>
        </div>
      </div>
      
      <!-- 连接测试 -->
      <div class="debug-section">
        <h3>连接测试</h3>
        <div class="test-buttons">
          <button @click="runConnectionTest" :disabled="isTesting">
            {{ isTesting ? '测试中...' : '运行连接测试' }}
          </button>
          <button @click="testLoginApi">测试登录API</button>
          <button @click="clearResults">清除结果</button>
        </div>
        
        <div v-if="connectionResults" class="test-results">
          <h4>测试结果:</h4>
          <div v-for="test in connectionResults.tests" :key="test.name" :class="['test-result', test.status]">
            <strong>{{ test.name }}:</strong> {{ test.message }}
          </div>
        </div>
      </div>
      
      <!-- 登录测试 -->
      <div class="debug-section">
        <h3>登录测试</h3>
        <div class="login-form">
          <div class="form-item">
            <label>用户名:</label>
            <input v-model="testUsername" type="text" placeholder="输入测试用户名" />
          </div>
          <div class="form-item">
            <label>密码:</label>
            <input v-model="testPassword" type="password" placeholder="输入测试密码" />
          </div>
          <button @click="runLoginTest" :disabled="isLoginTesting">
            {{ isLoginTesting ? '测试中...' : '测试登录' }}
          </button>
        </div>
        
        <div v-if="loginResult" :class="['login-result', loginResult.status]">
          <strong>登录测试结果:</strong> {{ loginResult.message }}
          <div v-if="loginResult.data" class="result-data">
            <pre>{{ JSON.stringify(loginResult.data, null, 2) }}</pre>
          </div>
        </div>
      </div>
      
      <!-- 错误信息 -->
      <div class="debug-section">
        <h3>错误信息</h3>
        <div v-if="errors.length === 0" class="no-errors">
          暂无错误
        </div>
        <div v-else class="error-list">
          <div v-for="(error, index) in errors" :key="index" class="error-item">
            <div class="error-time">{{ error.time }}</div>
            <div class="error-message">{{ error.message }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getApiStatus, testApiConnection, testLoginApi } from '@/utils/apiDebug'

const apiStatus = ref({})
const connectionResults = ref(null)
const loginResult = ref(null)
const errors = ref([])
const isTesting = ref(false)
const isLoginTesting = ref(false)
const testUsername = ref('admin')
const testPassword = ref('123456')

onMounted(() => {
  apiStatus.value = getApiStatus()
  addError('页面加载完成', 'info')
})

const addError = (message, type = 'error') => {
  errors.value.push({
    time: new Date().toLocaleTimeString(),
    message,
    type
  })
}

const runConnectionTest = async () => {
  isTesting.value = true
  try {
    connectionResults.value = await testApiConnection()
    addError('连接测试完成', 'success')
  } catch (error) {
    addError(`连接测试失败: ${error.message}`, 'error')
  } finally {
    isTesting.value = false
  }
}

const runLoginTest = async () => {
  isLoginTesting.value = true
  try {
    loginResult.value = await testLoginApi(testUsername.value, testPassword.value)
    if (loginResult.value.status === 'success') {
      addError('登录测试成功', 'success')
    } else {
      addError(`登录测试失败: ${loginResult.value.message}`, 'error')
    }
  } catch (error) {
    addError(`登录测试异常: ${error.message}`, 'error')
  } finally {
    isLoginTesting.value = false
  }
}

const clearResults = () => {
  connectionResults.value = null
  loginResult.value = null
  errors.value = []
  addError('结果已清除', 'info')
}
</script>

<style scoped>
.api-debug {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
  font-family: Arial, sans-serif;
}

h1 {
  color: #333;
  text-align: center;
  margin-bottom: 30px;
}

.debug-sections {
  display: grid;
  gap: 20px;
  margin-bottom: 30px;
}

.debug-section {
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 20px;
  background: #f9f9f9;
}

.debug-section h3 {
  margin-top: 0;
  color: #666;
  border-bottom: 1px solid #ddd;
  padding-bottom: 10px;
}

.info-grid {
  display: grid;
  gap: 8px;
}

.info-item {
  background: white;
  padding: 8px;
  border-radius: 4px;
  font-size: 14px;
}

.test-buttons {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 20px;
}

button {
  padding: 10px 20px;
  background: #007bff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

button:hover:not(:disabled) {
  background: #0056b3;
}

button:disabled {
  background: #6c757d;
  cursor: not-allowed;
}

.login-form {
  margin-bottom: 20px;
}

.form-item {
  margin-bottom: 15px;
}

.form-item label {
  display: block;
  margin-bottom: 5px;
  font-weight: bold;
}

.form-item input {
  width: 100%;
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

.test-results {
  margin-top: 15px;
}

.test-result {
  margin: 5px 0;
  padding: 5px;
  border-radius: 4px;
}

.test-result.success {
  color: #28a745;
  background: #d4edda;
}

.test-result.error {
  color: #dc3545;
  background: #f8d7da;
}

.login-result {
  margin-top: 15px;
  padding: 10px;
  border-radius: 4px;
}

.login-result.success {
  color: #28a745;
  background: #d4edda;
}

.login-result.error {
  color: #dc3545;
  background: #f8d7da;
}

.result-data {
  margin-top: 10px;
  background: #f8f9fa;
  padding: 10px;
  border-radius: 4px;
}

.result-data pre {
  margin: 0;
  font-size: 12px;
  overflow-x: auto;
}

.error-list {
  max-height: 300px;
  overflow-y: auto;
}

.error-item {
  background: #fff5f5;
  padding: 10px;
  margin: 5px 0;
  border-radius: 4px;
  border-left: 3px solid #dc3545;
}

.error-time {
  font-size: 12px;
  color: #666;
  margin-bottom: 5px;
}

.error-message {
  font-size: 14px;
}

.no-errors {
  text-align: center;
  color: #28a745;
  font-style: italic;
}
</style> 